<template>
  <div>
    <!-- 新增修改表单 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="DialogFormVisibleCom">
      <el-form ref="dataForm" :model="formSubmitDataCom" :rules="rules" label-position="right" label-width="100px"
               style="width: 90%; margin:10px auto;">

        <el-row :gutter="20">

          <!-- 访问源编码 -->
          <el-col :span="12">
            <el-form-item label="访问源编码" prop="acs_code">
              <el-input 
              v-model="formSubmitDataCom.acs_code"
              :disabled="true"
              />
            </el-form-item>
          </el-col>

          <!-- 访问源描述 -->
          <el-col :span="12">
            <el-form-item label="访问源描述" prop="acs_name">
              <el-input 
              v-model="formSubmitDataCom.acs_name"
              />
            </el-form-item>
          </el-col>

          <!-- 类型 -->
          <el-col :span="12">
            <el-form-item label="类型" prop="acs_type">
              <el-select
                v-model="formSubmitDataCom.acs_type"
                class="filter-item"
                placeholder="Please select"
                :disabled="dialogStatus==='update'"
              >
                <el-option v-for="items in acsTypeOptions" :key="items.value" :label="items.label" :value="items.value"/>
              </el-select>
            </el-form-item>
          </el-col>

          <!-- IP地址 -->
          <el-col :span="12">
            <el-form-item label="IP地址" prop="ip">
              <el-input 
              v-model="formSubmitDataCom.ip"
              />
            </el-form-item>
          </el-col>

          <!-- 端口 -->
          <el-col :span="12">
            <el-form-item label="端口" prop="port">
              <el-input 
              v-model="formSubmitDataCom.port"
              />
            </el-form-item>
          </el-col>

          <!-- 用户（数据库/服务器） -->
          <el-col :span="12">
            <el-form-item label="用户" prop="user_code">
              <el-input 
              v-model="formSubmitDataCom.user_code"
              />
            </el-form-item>
          </el-col>

          <!-- 密码（数据库/服务器） -->
          <el-col :span="12">
            <el-form-item label="密码" prop="pass">
              <el-input 
              v-model="formSubmitDataCom.pass"
              />
            </el-form-item>
          </el-col>


          <template v-if="this.formSubmitDataCom.acs_type==='1'">

          <!-- 实例名 -->
          <el-col :span="12">
            <el-form-item label="实例名" prop="inst_name">
              <el-input 
              v-model="formSubmitDataCom.inst_name"
              />
            </el-form-item>
          </el-col>

          <!-- 数据库类型 -->
          <el-col :span="12">
            <el-form-item label="数据库类型" prop="db_type">
              <el-select
                v-model="formSubmitDataCom.db_type"
                class="filter-item"
                placeholder="Please select"
                >
                <el-option v-for="items in dbTypeOptions" :key="items.value" :label="items.label" :value="items.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          </template>

          <!-- 法人代码 -->
          <el-col :span="12">
            <el-form-item label="法人代码" prop="lp_code">
              <el-select
                v-model="formSubmitDataCom.lp_code"
                class="filter-item"
                placeholder="Please select"
                :disabled="true"
                >
                <el-option v-for="items in lpOptions" :key="items.lp_code" :label="items.lp_name" :value="items.lp_code"/>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="状态">
                <el-radio
                  v-model="formSubmitDataCom.status"
                  label="1"
                >启用
                </el-radio>
                <el-radio
                  v-model="formSubmitDataCom.status"
                  label="0"
                >停用
                </el-radio>
            </el-form-item>
          </el-col>


        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button @click="resetFormData">
          重置
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          提交
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// import PageHeaderLayout from '@/layouts/PageHeaderLayout'
// import ApeDrawer from '@/components/ApeDrawer'
// import { mapGetters } from 'vuex'
// const calendarTypeOptions = [
//   {key: 'CN', display_name: 'China'},
//   {key: 'US', display_name: 'USA'},
//   {key: 'JP', display_name: 'Japan'},
//   {key: 'EU', display_name: 'Eurozone'}
// ]

// // arr to obj, such as { CN : "China", US : "USA" }
// const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
//   acc[cur.key] = cur.display_name
//   return acc
// }, {})


export default {
  name: 'submitForm',
  components: {
    //PageHeaderLayout,
  },
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false
    },
    dialogStatus: {
      type: String,
    },
    lpOptions: {
        type: Array,
        default() {
            return []
        }
    },
    acsTypeOptions: {
        type: Array,
        default() {
            return []
        }
    },
    formSubmitData: {
      type: Object,
      default: function () {
        return {
          id: undefined,
          acs_code: "",
          acs_name: "",
          acs_type: "",
          ip: "",
          port: "",
          user_code: "",
          pass: "",
          inst_name: "",
          db_type: "",
          lp_code: "",
          status: "",
          remark : "",
        }
      }
    }
  },
  // 定义属性
  data() {
    return {
      textMap: {
        update: '修改',
        create: '新增'
      },
      rules: {
        // theme: [{required: true, message: '主题必填', trigger: 'change'}],
        // detail: [{required: true, message: '内容必填', trigger: 'change'}],
        // s_date: [{required: true, message: '生效日期必填', trigger: 'change'}],
        // e_date: [{required: true, message: '失效日期必填', trigger: 'change'}],
      },
      // 表单用到的值
      // calendarTypeOptions,
      // 1：mysql 2:oracle 3:db2 4:hive
      dbTypeOptions:[
        {
          label:'mysql',
          value:'1'
        },
        {
          label:'oracle',
          value:'2'
        },
        {
          label:'db2',
          value:'3'
        },
        {
          label:'hive',
          value:'4'
        }
      ]
    }
  },
  // 计算属性，会监听依赖属性值随之变化
  computed: {
    DialogFormVisibleCom: {
      get() {
        return this.dialogFormVisible
      },
      set(val) {
        this.$emit('update:dialogFormVisible', val)
      }
    },
    formSubmitDataCom: {
      get() {
        return this.formSubmitData
      },
      set(val) {
        this.$emit('update:formSubmitData', val)
      }
    },
  },
  // 监控data中的数据变化
  watch: {
  },
  // 方法集合
  methods: {
    resetFormData() {
      this.$emit('resetFormDataEmit')
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$emit('handleFromCreateDataEmit')
        }
      })
    },

    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          // const tempData = Object.assign({}, this.formSubmitData)
          this.$delete(this.formSubmitDataCom, "_XID");
          this.$emit('handleFromUpdateDataEmit')
        }
      })
    },

  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {

  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {

  },
  beforeCreate() {
  }, // 生命周期 - 创建之前
  beforeMount() {
  }, // 生命周期 - 挂载之前
  beforeUpdate() {
  }, // 生命周期 - 更新之前
  updated() {
  }, // 生命周期 - 更新之后
  beforeDestroy() {
  }, // 生命周期 - 销毁之前
  destroyed() {
  }, // 生命周期 - 销毁完成
  activated() {
  }, // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style lang='scss' scoped>

</style>